package com.robot.member.controller;

import com.alibaba.fastjson.JSON;
import com.robot.member.pojo.Member;
import com.robot.member.service.MemberService;
import entity.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * member控制器层
 *
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/member")
public class MemberJsonController {
    @Autowired
    private MemberService memberService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 传入对象来修改member
     *
     * @param member
     */
    @PutMapping(value = "third/updateMemberByMember", produces = "application/json; charset=utf-8")
    @Transactional(rollbackFor = Exception.class)
    public void thirdUpdateMemberByMember(@RequestBody String member) {
        memberService.update(JSON.parseObject(member, Member.class));
    }


    /**
     * 通过id查询会员
     *
     * @param id
     * @return
     */
    @GetMapping(value = "third/findMemberById/{id}", produces = "application/json; charset=utf-8")
    public String thirdFindMemberById(@PathVariable String id) {
        return JSON.toJSONString(memberService.findById(id));
    }

    @RequestMapping(value = "third/findEnableMemberById/{id}", produces = "application/json;charset=utf-8")
    public String thirdFindEnableMemberById(@PathVariable String id) {
        return JSON.toJSONString(memberService.findByIdAndStatus(id, "1"));
    }

    /**
     * 会员列表并分页
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "third/findMembers/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindMembers(@PathVariable int page, @PathVariable int size) {
        Page<Member> pageList = memberService.findAll(page, size);
        return JSON.toJSONString(new PageResult<Member>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 导出下级/我的团队队员列表并分页/邀请人ID是我的全部会员
     *
     * @param id
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "third/findMembersByRecommendId/{id}/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindMembersByRecommendId(@PathVariable String id, @PathVariable int page, @PathVariable int size) {
        Page<Member> pageData = memberService.findAllByRecommendIdOrderByCreatedAtDesc(id, page, size);
        return JSON.toJSONString(new PageResult<Member>(pageData.getTotalElements(), pageData.getContent()));
    }

    /**
     * 查找会员推荐人名字
     *
     * @param recommendId
     * @return
     */
    @RequestMapping(value = "third/findRecommenderName/{recommendId}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindRecommenderName(@PathVariable String recommendId) {
        return JSON.toJSONString(memberService.findRecommenderName(recommendId));

    }

    /**
     * 查找会员推荐人数
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "third/findRecommendedNumber/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public Integer thirdFindRecommendedNumber(@PathVariable String id) {
        return memberService.findRecommendedNumber(id);
    }

    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @PostMapping(value = "third/findSearch/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindSearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Member> pageList = memberService.findSearch(searchMap, page, size);
        return JSON.toJSONString(new PageResult<Member>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 通过推荐人名字查询推荐列表
     *
     * @param recommendName
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findMembersByRecommendName/{page}/{size}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String thirdFindMembersByRecommendName(@RequestBody String recommendName, @PathVariable int page, @PathVariable int size) {
        Page<Member> pageList = memberService.findAllByRecommendName(recommendName, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 通过推荐人手机号查询推荐列表
     *
     * @param recommendPhone
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findMembersByRecommendPhone/{page}/{size}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String thirdFindMembersByRecommendPhone(@RequestBody String recommendPhone, @PathVariable int page, @PathVariable int size) {
        Page<Member> pageList = memberService.findAllByRecommendPhone(recommendPhone, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }


    /**
     * 根据isPlus查找今日会员
     *
     * @param isPlus
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findMembersByIsPlusAndToday/{page}/{size}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String thirdFindMembersByIsPlusAndToday(@RequestBody String isPlus, @PathVariable int page, @PathVariable int size) {
        Page<Member> pageList = memberService.findAllByIsPlusAndToday(isPlus, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据isPlus查找昨天会员
     *
     * @param isPlus
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findMembersByIsPlusAndYesterday/{page}/{size}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String thirdFindMembersByIsPlusAndYesterday(@RequestBody String isPlus, @PathVariable int page, @PathVariable int size) {
        Page<Member> pageList = memberService.findAllByIsPlusAndYesterday(isPlus, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据isPlus查找最近7日会员
     *
     * @param isPlus
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findMembersByIsPlusAndWeek/{page}/{size}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String thirdFindMembersByIsPlusAndWeek(@RequestBody String isPlus, @PathVariable int page, @PathVariable int size) {
        Page<Member> pageList = memberService.findAllByIsPlusAndWeek(isPlus, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据isPlus和时间段查找总会员
     * startTime和endTime格式是yyyy-MM-dd即2020-03-01
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findMembersByIsPlusAndBetweenAnd/{page}/{size}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String thirdFindMembersByIsPlusAndBetweenAnd(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        String isPlus = (String) searchMap.get("isPlus");
        String startTime = (String) searchMap.get("startTime") + " 00:00:00";
        String endTime = (String) searchMap.get("endTime") + " 23:59:59";
        Page<Member> pageList = memberService.findAllByIsPlusAndBetweenAnd(isPlus, startTime, endTime, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据isPlus查找总会员
     *
     * @param isPlus
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findMembersByIsPlusAndTotal/{page}/{size}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String thirdFindMembersByIsPlusAndTotal(@RequestBody String isPlus, @PathVariable int page, @PathVariable int size) {
        Page<Member> pageList = memberService.findAllByIsPlusAndTotal(isPlus, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }
}